﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Nop.Admin.Models.WindAlert;
using Nop.Core;
using Nop.Services.Helpers;
using Nop.Services.Localization;
using Nop.Services.Security;
using Nop.Services.WindAlert;
using Nop.Web.Framework.Controllers;
using Nop.Web.Framework.Kendoui;

namespace Nop.Admin.Controllers
{
    public partial class WaveDataController : BaseAdminController
    {
        private readonly IWaveDataService _waveDataService;
        private readonly ISpotService _spotService;
        private readonly IWorkContext _workContext;
        private readonly ILocalizationService _localizationService;
        private readonly IDateTimeHelper _dateTimeHelper;
        private readonly IPermissionService _permissionService;

        public WaveDataController(IWaveDataService waveDataService,ISpotService SpotService, IWorkContext workContext,
            ILocalizationService localizationService, IDateTimeHelper dateTimeHelper,
            IPermissionService permissionService)
        {
            this._waveDataService = waveDataService;
            this._spotService = SpotService;
            this._workContext = workContext;
            this._localizationService = localizationService;
            this._dateTimeHelper = dateTimeHelper;
            this._permissionService = permissionService;
        }

        #region methods
        public ActionResult Index()
        {
            return RedirectToAction("List");
        }

        public ActionResult List()
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSystemLog))
                return AccessDeniedView();
            var model = new WaveDataListModel();
            return View(model);
        }

        [HttpPost]
        public ActionResult WaveDataList(DataSourceRequest command, WaveDataListModel model)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSystemLog))
                return AccessDeniedView();

            DateTime? createdOnFromValue = (model.CreatedOnFrom == null) ? null
                            : (DateTime?)_dateTimeHelper.ConvertToUtcTime(model.CreatedOnFrom.Value, _dateTimeHelper.CurrentTimeZone);

            DateTime? createdToFromValue = (model.CreatedOnTo == null) ? null
                            : (DateTime?)_dateTimeHelper.ConvertToUtcTime(model.CreatedOnTo.Value, _dateTimeHelper.CurrentTimeZone).AddDays(1);


            var WaveDataItems = _waveDataService.GetAllWaveData(createdOnFromValue, createdToFromValue, command.Page - 1, command.PageSize);
            var gridModel = new DataSourceResult
            {
                Data = WaveDataItems.Select(x =>
                {
                    return new WaveDataModel()
                    {
                        Id = x.Id,
                        CreatedOnUtc  = x.CreatedOnUtc,
                        WaveTime = x.WaveTimeUTC,
                        WaveAdjustedTime = x.WaveAdjustedTime,
                        Hmax  = x.Hmax,
                        Hs = x.Hs,
                        Hthird = x.Hthird,
                        Direction = x.Direction,
                        Tav = x.Tav,
                        Tz = x.Tz,
                        Tp = x.Tp,
                        Temperture = x.Temperture,
                        SpotName = _spotService.GetSpotById((int)x.SpotId).Name,
                    };
                }),
                Total = WaveDataItems.TotalCount
            };
            return new JsonResult
            {
                Data = gridModel
            };
        }
        
        [HttpPost, ActionName("List")]
        [FormValueRequired("clearall")]
        public ActionResult ClearAll()
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSystemLog))
                return AccessDeniedView();

            _waveDataService.ClearAll();

            SuccessNotification(_localizationService.GetResource("Admin.System.Log.Cleared"));
            return RedirectToAction("List");
        }

        public ActionResult View(int id)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSystemLog))
                return AccessDeniedView();

            var waveData= _waveDataService.GetWaveDataById(id);
            if (waveData == null)
                //No waveData found with the specified id
                return RedirectToAction("List");

            var model = new WaveDataModel()
            {
                Id = waveData.Id,
                CreatedOnUtc = waveData.CreatedOnUtc,
                WaveTime = waveData.WaveTimeUTC,
                WaveAdjustedTime = waveData.WaveAdjustedTime,
                Hmax = waveData.Hmax,
                Hs = waveData.Hs,
                Hthird = waveData.Hthird,
                Direction = waveData.Direction,
                Tav = waveData.Tav,
                Tz = waveData.Tz,
                Tp = waveData.Tp,
                Temperture = waveData.Temperture,
                SpotName = _spotService.GetSpotById((int)waveData.SpotId).Name,
            };

            return View(model);
        }

        [HttpPost]
       public ActionResult Delete(int id)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSystemLog))
                return AccessDeniedView();

            var waveData = _waveDataService.GetWaveDataById(id);
            if (waveData == null)
                //No waveata found with the specified id
                return RedirectToAction("List");

            _waveDataService.DeleteWaveData(waveData);

            SuccessNotification(_localizationService.GetResource("Admin.System.Log.Deleted"));
            return RedirectToAction("List");
        }

        [HttpPost]
        public ActionResult DeleteSelected(ICollection<int> selectedIds)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSystemLog))
                return AccessDeniedView();

            if (selectedIds != null)
            {
                var waveDataItems = _waveDataService.GetWaveDataByIds(selectedIds.ToArray());
                foreach (var waveDataItem in waveDataItems)
                    _waveDataService.DeleteWaveData(waveDataItem);
            }

            return Json(new { Result = true});
        }
        #endregion

        
    }
}